package com.mxprojects.io
{
	import com.mxprojects.gui.DefaultDialog;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	import com.mxprojects.views.Forums;
	import com.mxprojects.views.LoginDialog;
	import com.mxprojects.views.Posts;
	import com.mxprojects.views.Threads;
	
	public class UserControl
	{		
		private static var user:Object = new Object();
		private static var loadLogin:ServerHandler = new ServerHandler();
		private static var loginData:Object;
		private static var isAutoLogin:Boolean;
		
		public static var loggedIn:Boolean = false;
		
		public function UserControl(){ }
		
		public static function login(username:String, password:String, autoLogin:Boolean = false):void {
			//take the stuff, pass it to the server and see if we have a match
			user.username = username;
			user.password = password;
			user.autoLogin = autoLogin;
			user.privateMessageLimit = Config.PRIVATE_MESSAGE_LIMIT;
			user.guestGroupID = Config.GUEST_GROUP_ID;
			
			//set the parameter for auto-login
			isAutoLogin = autoLogin;
			
			//if we are in threads or posts let's also send the forumID so we can get the user's perms
			if(MXP3.currentLocation == Word.THREADS) {
				user.forumID = Threads.threadData.forumID;
			}else if( MXP3.currentLocation == Word.POSTS) {
				user.forumID = Posts.postData.forumID;
			}
			
			loadLogin.transmitData("login.php", user);
			loadLogin.listenForData(processLogin);
		}
		
		public static function logout():void {
			BoardCookie.clearCookie();
			BoardCookie.loadCookie();
			loggedIn = false;
			
			//force a reset of the forum
			if(MXP3.currentLocation == Word.FORUMS){
				Forums.load();
			}else{
				DeepLinking.setLink(Word.FORUMS, DeepLinking.createLinkArray());
			}
		}
		
		private static function processLogin(event:CustomEvent):void {
			loginData = event.getServerData();
			
			if(loginData.loginResult == "good"){
				loggedIn = true;
				
				//set the user up
				MXP3.header.showLoggedInUser(loginData.username, loginData.privateTotal, loginData.privateNew, loginData.admin);
				
				//save the username and password into a cookie
				BoardCookie.saveCookie(loginData.username, loginData.password, loginData.userID);
				
				//close the login dialog if it's up
				LoginDialog.close();
				
				//check to see if we are in an area that we can post in
				Permissions.setup(loginData.isAdmin, loginData.isModerator, loginData.isAllowedToPost);
			}else{
				//throw an error about the funk password and re-listen to the keys
				if(LoginDialog.loginDialog != null){
					LoginDialog.listenToKeys();
					DefaultDialog.show(Language.main(Word.ERROR_LOGIN_INVALID), false);
				}else{
					//this must mean the board cookie is outdated. So kill it.
					BoardCookie.clearCookie();
					MXP3.header.showDefaultUserInfo();
				}
			}
			
			if(isAutoLogin){
				//setup the deep linking and load the forum
				DeepLinking.setup();
			}
		}
	}
}